<?php
/*
# TCE - Tight Control Environment: Hosting Platform
# Copyright (c) 2006 by the TightControl Dev Team
# http://tce.mystichost.net
#
#
# License:
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the MPL Mozilla Public License
#    as published by the Free Software Foundation; either version 1.1
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    MPL Mozilla Public License for more details.
#
#    You may have received a copy of the MPL Mozilla Public License
#    along with this program.
#
#    An on-line copy of the MPL Mozilla Public License can be found
#    http://www.mozilla.org/MPL/MPL-1.1.html
#
#
# The TCE Home Page is at:
#
#    http://tce.mystichost.net
*/

/* Check if the master ftp controller has been loaded */
if (!class_exists('CMD_Controller')){
	throw new Exception('The CMD_Controller abstraction layer has not been found!');
}

/**
 * Sudo CMD Controller class, run our super (TCE_ROOT/sbin) scripts as root (the scripts have to lower themselve theirselves)
 *
 */
class CMD_Sudo_Controller extends CMD_Controller {
	private $Configuration = false; // Or replace with default :)
	/**
	 * Runs $Filename with arguments $Arguments
	 * 
	 * Be noted that this should only allow commands run from the TCE_ROOT/sbin folder !
	 *
	 * @param string $Filename
	 * @param array $Arguments
	 * @return string The Output
	 */
	function runCmd($Filename, $Arguments = array(), &$ReturnVal = 0){
		$Filename = escapeshellcmd($Filename);
		$Command = "$Filename";
		for ($i=0;$i<count($Arguments); $i++){
			$Command .= " " . escapeshellarg($Arguments[$i]);
		}
		$Output = array();
		exec  ($Command, $Output, $ReturnVal);	
		$Output = implode("\n", $Output);
		return $Output;	
	}
	/**
	 * Returns an array of the information that is needed to handle the CMD-layer
	 * 
	 * Example result:
	 * 
	 * $Array[0] = array('Field' => 'CMD_SUDO', 'Name' => 'Path to sudo', 'Description' => 'The full path to the sudo executable', 'Required' => true, 'Default' => '/usr/bin/sudo')
	 * 
	 * @return array
	 */
	function getConfigFields(){
		$ConfigArr = array();
		$ConfigArr[] = array('Field' => 'CMD_SUDO','Name' =>  'Path to sudo', 'Description' => 'The full path to the sudo command', 'Required' => true, 'Default' => '/usr/bin/sudo');
		return $ConfigArr;
	}
	/**
	 * Sets the configuration
	 * 
	 * To know what kind of configuration options there are, check getConfigFields()
	 *
	 * @param array $Configuration
	 * @return boolean
	 */
	function setConfigFields($Configuration = false){
		$this->Configuration = $Configuration;
		// TODO Validate $Configuration and be sure that the required fields are set, otherwise return false or throw exception (sinnerg, 13/10/2006)
	}
	/**
	 * Ask to start interfacing with the CMD-layer using the configuration 
	 * set by setConfigFields  OR by supplying $Configuration
	 *
	 * @param variable $Configuration
	 * @return boolean
	 */
	function startInterface($Configuration = false){
		if ($Configuration){
			$this->setConfigFields($Configuration);
		}
	}
	/**
	 * Stop interfacing with the CMD-layer
	 * 
	 * Should be called in a __destruct() function 
	 * 
	 * @return boolean
	 */
	function endInterface(){
		return true;
	}
	/**
	 * Should return an array like this return array('Name' => 'CMD Controller', 'Description' => 'A CMD Controller');
	 *
	 * @return array
	 */
	function getControllerInfo(){
		return array('Name' => 'Sudo', 'Description' => 'Uses the sudo command to elevate the rights of the super scripts stored in the TCE /sbin');
	}
}
/* Register it to the CMD_Controller */
CMD_Controller::Register('CMD_Sudo_Controller');

?>